package tree;

/**
 * @author : Rookie
 * @description : 对称二叉树
 * @date : 2023/10/11
 */
public class SymmetricalBinaryTree {

    public static void main(String[] args) {
        int[] input = {1, 2, 2, 3, 4, 4, 3, 5, -1, -1, 6, 6, -1, -1, 5};
        TreeNode root = BinaryTreeUtil.buildTree(input);
        if (root == null) {
            return;
        }
        System.out.println("二叉树是否对称：" + isInvertedTree(root.left,root.right));
    }

    private static boolean isInvertedTree(TreeNode left,TreeNode right) {
        if (left == null && right != null) return false;
        else if (left != null && right == null) return false;
        else if (left == null && right == null) return true;
        else if (left.element == right.element) return true;

        // 判断树的外侧是否可翻转
        boolean outside = isInvertedTree(left.left,right.right);
        // 判断树的内测是否可翻转
        boolean inside = isInvertedTree(left.right,right.left);
        return outside && inside;
    }
}
